﻿using Corina.Commands;
using Corina.Datamodel.Entities;
using Corina.Infrastructure;
using Corina.Win.ServerHub.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Corina.Win.ServerHub.Viewmodels
{
    public class DataContainer : BaseClass
    {
        public DataContainer()
        {
            ClientsCollection.CollectionChanged += CollectionChanged;
        }

        private void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            ClientCollectionChanged.Invoke(this, EventArgs.Empty);
        }

        private string _viewUrl = "pack://application:,,,/Corina.Win.ServerHub;component/Views/LoginView.xaml";
        public string ViewUrl
        {
            get { return _viewUrl; }
            set
            {
                _viewUrl = value;
                RaisePropertyChanged(() => ViewUrl);
            }
        }

        private ManagerInfo _currentAccount;
        public ManagerInfo CurrentAccount
        {
            get
            {
                return _currentAccount;
            }
            set
            {
                _currentAccount = value;
                RaisePropertyChanged(() => CurrentAccount);
                RaisePropertyChanged(() => IsValid);
                AccountChanged.Invoke(this, EventArgs.Empty);
            }
        }

        public bool IsValid
        {
            get { return CurrentAccount != null; }
        }

        private bool _isServerRuning = false;
        public bool IsServerRunning
        {
            get { return _isServerRuning; }
            set
            {
                _isServerRuning = value;
                RaisePropertyChanged(() => IsServerRunning);
            }
        }

        public event EventHandler<EventArgs> AccountChanged = delegate { };
        public event EventHandler<EventArgs> ClientCollectionChanged = delegate { };
        public event EventHandler<EventArgs> RunlogChanged = delegate { };
        public event EventHandler<EventArgs> ServerStatusChanged = delegate { };

        #region Client
        private ObservableCollection<ClientInfo> _clientsCollection = new ObservableCollection<ClientInfo>();
        public ObservableCollection<ClientInfo> ClientsCollection
        {
            get { return _clientsCollection; }
            set
            {
                _clientsCollection = value;
                RaisePropertyChanged(() => ClientsCollection);
                //ChangeClient();
                ClientCollectionChanged.Invoke(this, EventArgs.Empty);
            }
        }
        #endregion

        #region Logs
        private StringBuilder _runlog = new StringBuilder();
        public StringBuilder Runlog
        {
            get { return _runlog; }
            set
            {
                _runlog = value;
                RaisePropertyChanged(() => Runlog);
            }
        }

        public void WriteRunlog(params object[] paras)
        {
            //foreach(object obj in paras)
            //{
            //    Runlog.AppendFormat("[{0}] {1}\r\n", DateTime.Now, obj);
            //}
            string content = string.Join("--> ", paras);
            Runlog.AppendFormat("[{0}] {1}\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt"), content);
            RunlogChanged.Invoke(this, EventArgs.Empty);
        }
        #endregion

        #region Prompt
        private string _promptContent = string.Empty;
        public string PromptContent
        {
            get { return _promptContent; }
            set
            {
                _promptContent = value;
                RaisePropertyChanged(() => PromptContent);
            }
        }

        private string _promptHeader = string.Empty;
        public string PromptHeader
        {
            get { return _promptHeader; }
            set
            {
                _promptHeader = value;
                RaisePropertyChanged(() => PromptHeader);
            }
        }

        private bool _showPromptWindow = false;
        public bool ShowPromptWindow
        {
            get { return _showPromptWindow; }
            set
            {
                _showPromptWindow = value;
                RaisePropertyChanged(() => ShowPromptWindow);
            }
        }

        public ICommand HidePromptCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        ShowPromptWindow = false;
                    }
                };
            }
        }
        #endregion
    }
}
